138. 复制带随机指针的链表

https://leetcode.cn/problems/copy-list-with-random-pointer/description/

struct Node* copyRandomList(struct Node* head) 
{
    if(head==NULL)
    {
        return NULL;
    }
    //在原有链表的每个结点上拷贝一个结点
    struct Node*cur=head;
    while(cur)
    {
        struct Node*newnode=(struct Node*)malloc(sizeof(struct Node));
        newnode->val=cur->val;
        newnode->next=cur->next;
        cur->next=newnode;
        cur=newnode->next;
    }

    //链接随机结点
    cur=head;
    while(cur)
    {
        struct Node*newnode=cur->next;
        if(cur->random)
        {
            newnode->random=cur->random->next;
        }
        else
        {
            newnode->random=NULL;
        }
        cur=cur->next->next;
    }


    struct Node* headNew = head->next;
    for (struct Node* node = head; node != NULL; node = node->next) 
    {
        struct Node* nodeNew = node->next;
        node->next = node->next->next;
        nodeNew->next = (nodeNew->next != NULL) ? nodeNew->next->next : NULL;
    }

	return headNew;
}


